package com.example.pc.mytestapplication.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.widget.AdapterView;

import com.example.pc.mytestapplication.R;
import com.example.pc.mytestapplication.adapter.AdapterMain;
import com.example.pc.mytestapplication.data.MainData;
import com.example.pc.mytestapplication.databinding.ActivityConversionBinding;
import com.example.pc.mytestapplication.test.TestConversionActivity;
import com.example.pc.mytestapplication.util.TitleBarUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by lxg on 2018/1/25.
 */

public class ConversionActivity extends BaseActivity {

    private ActivityConversionBinding binding;
    private List<MainData> mainData;
    private AdapterMain adapterMain;

    @Override
    protected void loadView() {
        binding = DataBindingUtil.setContentView(ConversionActivity.this, R.layout.activity_conversion);
        TitleBarUtils.setTitle(ConversionActivity.this, "变换操作符", true);
    }

    @Override
    protected void setinitView() {
        binding.btConversion.setOnClickListener(this);
        binding.btTestConversion.setOnClickListener(this);
        binding.lvConverion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                switch (i) {
                    case 0:
                        Observable.create(new ObservableOnSubscribe<Integer>() {
                            @Override
                            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                                emitter.onNext(10086);
                                emitter.onNext(10010);
                                emitter.onNext(10011);
                                emitter.onComplete();
                            }
                        }).map(new Function<Integer, String>() {
                            @Override
                            public String apply(Integer integer) throws Exception {
                                switch (integer) {
                                    case 10086:
                                        return "这是移动的电话号" + 10086;
                                    case 10010:
                                        return "这是电信的电话号" + 10010;
                                    case 10011:
                                        return "这是联通的电话号" + 10011;
                                    default:
                                        return "这个我也不知道呢";
                                }
                            }
                        }).subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String s) {
                                if (mainData.get(i).getTextType().startsWith("未处理")) {
                                    mainData.get(i).setTextType("");
                                }
                                mainData.get(i).setTextType(mainData.get(i).getTextType() + s);
                                adapterMain.notifyDataSetChanged();
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                        break;
                    case 1:
                        Observable.create(new ObservableOnSubscribe<Integer>() {
                            @Override
                            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                                emitter.onNext(1);
                                emitter.onNext(2);
                                emitter.onNext(3);
                                emitter.onNext(4);
                                emitter.onNext(5);
                                emitter.onNext(6);
                                emitter.onComplete();
                            }
                        }).flatMap(new Function<Integer, ObservableSource<Float>>() {
                            @Override
                            public ObservableSource<Float> apply(Integer integer) throws Exception {
                                switch (integer) {
                                    case 1:
                                        return Observable.just(1.1F, 1.2F, 1.3F);
                                    case 2:
                                        return Observable.just(2.1F, 2.2F, 2.3F).delay(500, TimeUnit.MILLISECONDS);
                                    case 3:
                                        return Observable.just(3.1F, 3.2F, 3.3F);
                                    case 4:
                                        return Observable.just(4.1F, 4.2F, 4.3F);
                                    case 5:
                                        return Observable.just(5.1F, 5.2F, 5.3F);
                                    case 6:
                                        return Observable.just(6.1F, 6.2F, 6.3F);
                                    default:
                                        return Observable.just(0.1F, 0.2F, 0.3F);
                                }
                            }
                        }).subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<Float>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(Float aFloat) {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + aFloat + "___");
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + "传输按1-6,处理时2由于延时执行而被最后执行到");
                                        adapterMain.notifyDataSetChanged();
                                    }
                                });
                        break;
                    case 2:
                        Observable.create(new ObservableOnSubscribe<Integer>() {
                            @Override
                            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                                emitter.onNext(1);
                                emitter.onNext(2);
                                emitter.onNext(3);
                                emitter.onNext(4);
                                emitter.onNext(5);
                                emitter.onNext(6);
                                emitter.onComplete();
                            }
                        }).concatMap(new Function<Integer, ObservableSource<Float>>() {
                            @Override
                            public ObservableSource<Float> apply(Integer integer) throws Exception {
                                switch (integer) {
                                    case 1:
                                        return Observable.just(1.1F, 1.2F, 1.3F);
                                    case 2:
                                        return Observable.just(2.1F, 2.2F, 2.3F).delay(500, TimeUnit.MILLISECONDS);
                                    case 3:
                                        return Observable.just(3.1F, 3.2F, 3.3F);
                                    case 4:
                                        return Observable.just(4.1F, 4.2F, 4.3F);
                                    case 5:
                                        return Observable.just(5.1F, 5.2F, 5.3F);
                                    case 6:
                                        return Observable.just(6.1F, 6.2F, 6.3F);
                                    default:
                                        return Observable.just(0.1F, 0.2F, 0.3F);
                                }
                            }
                        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Float>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Float aFloat) {
                                if (mainData.get(i).getTextType().startsWith("未处理")) {
                                    mainData.get(i).setTextType("");
                                }
                                mainData.get(i).setTextType(mainData.get(i).getTextType() + aFloat + "___");
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType(mainData.get(i).getTextType() + "传输按1-6,处理时即使由于2延时执行,顺序仍然不变,后续事件等待2执行完毕后才会继续执行");
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 3:
                        Observable.create(new ObservableOnSubscribe<Integer>() {
                            @Override
                            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                                emitter.onNext(1);
                                emitter.onNext(2);
                                emitter.onNext(3);
                                emitter.onNext(4);
                                emitter.onNext(5);
                                emitter.onNext(6);
                                emitter.onNext(7);
                                emitter.onComplete();
                            }
                        }).buffer(3, 2).subscribe(new Observer<List<Integer>>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(List<Integer> integers) {
                                if (mainData.get(i).getTextType().startsWith("未处理")) {
                                    mainData.get(i).setTextType("");
                                }
                                mainData.get(i).setTextType(mainData.get(i).getTextType() + integers + "___");
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType(mainData.get(i).getTextType() + "传输1-7,缓存区大小为三,步长为2");
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_conversion:
                startActivity(new Intent(this, GroupActivity.class));
                break;
            case R.id.bt_test_conversion:
                startActivity(new Intent(this, TestConversionActivity.class));
                break;
        }
    }

    @Override
    protected void loadData() {
        mainData = new ArrayList<>();
        mainData.add(new MainData("这是一个使用map方法将integer转换成String的案例", "未处理"));
        mainData.add(new MainData("这是一个使用FlatMap方法将一个序列拆解成多个无序序列的案例", "未处理"));
        mainData.add(new MainData("这是一个使用concatMap方法将一个序列拆解成多个有序序列的案例", "未处理"));
        mainData.add(new MainData("这是一个使用buffer方法将一个序列缓存成多个序列,按指定数量分批发布的案例", "未处理"));
        adapterMain = new AdapterMain(this, mainData);
        binding.lvConverion.setAdapter(adapterMain);
    }
}
